package api

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var PromoteMgr = &promoteMgrApi{}

type promoteMgrApi struct{}

// @summary 推广人员列表
// @tags 工具
// @produce json
// @param   id formData  string false "推广人员id"
// @param   promote formData  string false "邀请码"
// @param   name formData  string false "推广人员名"
// @param   cur formData  int false "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /tool/promote/list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *promoteMgrApi) List(r *ghttp.Request) {
	var req model.PromoteListReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	data, total := service.Promote.GetPromoteDataList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 获取推广链接
// @tags 工具
// @produce json
// @router  /tool/promote/get-promote-link [GET]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *promoteMgrApi) GetPromoteLink(r *ghttp.Request) {
	link := "https://www.props-trader.com/share/index.html?invitation="
	response.JsonSuc(r, link)
}

// @summary 创建或编辑推广人员列表
// @tags 工具
// @produce json
// @param   id formData  int false "推广人员id,创建时不需要传入，更新时必须传入"
// @param   promote formData  string false "邀请码, 创建时必须传入，更新时不必"
// @param   name formData  string true "备注"
// @param   state formData  string true "状态"
// @param   updateBy formData  string false "编辑人ID，更新时必须传入，创建时不必"
// @param   createdAt formData  int false "创建时间，更新时必须传入，创建时不必"
// @param   createdBy formData  int false "创建人，创建时必须传入，更新时不必"
// @param   way formData  int true "方式: 0 创建 1更新"
// @router  /tool/promote/edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *promoteMgrApi) Edit(r *ghttp.Request) {
	var req model.PromoteEditReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	u := service.Context.GetUser(r.Context())

	var data = req

	if req.Way == 0 {
		if has := dao.PromoteData.HasPromoteDataOneByPromote(req.Promote); has {
			defErr.Panic(define.Fail, "标识不能重复")
		}
		req.CreatedBy = u.Id
	} else {
		//更新
		if len(req.Promote) >= 5 {
			defErr.Panic(define.Fail, "标识长度不得超过5位")
		}
		req.UpdatedBy = u.Id
		res := dao.PromoteData.GetPromoteDataOne(req.Id)
		if err := gconv.Struct(data, res); err != nil {
			defErr.Panic(define.Fail)
		}
		req.CreatedAt = data.CreatedBy
	}

	if _, err := dao.PromoteData.FieldsEx("Way").Save(data); err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	response.JsonSuc(r)

}

// @summary 批量删除推广人员
// @tags 工具
// @produce json
// @param   ids formData  string true "推广人员id列表, 13, 14"
// @router  /tool/promote/del [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *promoteMgrApi) Del(r *ghttp.Request) {
	var req model.DelPromoteReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	if ok := service.Promote.DeletePromoteList(req.Ids); ok {
		response.JsonSuc(r)
	}

	response.JsonFail(r)
}
